import { Component, OnInit } from '@angular/core';
import * as moment from 'moment';
import {AccountService} from '../account.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';

@Component({
  selector: 'app-top-up',
  templateUrl: './top-up.component.html',
  styleUrls: ['./top-up.component.scss'],
  providers: [AccountService]
})
export class TopUpComponent implements OnInit {

  _allChecked = false;
  _indeterminate = false;
  checkedIds = [];
  listData = [];
  pay_type = 0;
  _startDate = null;
  _endDate = null;
  startDate = null;
  endDate = null;
  sort = {price: null, pay_time: null};
  filterStatus = [
    {name: '充值成功', checked: false, value: 1},
    {name: '充值失败', checked: false, value: 2},
  ];

  loading = false;
  total;
  page = 1;
  pageSize = 10;

  constructor(private accountService: AccountService,
              private confirmServ: NzModalService,
              private _message: NzMessageService,
              ) { }

  _refreshStatus(e?, id?) {
    if (e) {
      this.checkedIds.push(id);
    } else {
      this.checkedIds.every((val, idx, array) => {
        if (id === val) {
          this.checkedIds.splice(idx, 1);
          return false;
        }
        return true;
      });
    }
    const allChecked = this.listData.every(value => value.checked === true);
    const allUnChecked = this.listData.every(value => !value.checked);
    this._allChecked = allChecked;
    this._indeterminate = (!allChecked) && (!allUnChecked);
  };

  _checkAll(value) {
    if (value) {
      this.listData.every((val, idx, array) => {
        if (!val.checked) {
          val.checked = true;
          this.checkedIds.push(val.order_no);
        }
        return true;
      });
    } else {
      this.listData.every((val, idx, array) => {
        if (val.checked) {
          val.checked = false;
          this.checkedIds.every((val1, idx1, array1) => {
            if (val.order_no === val1) {
              this.checkedIds.splice(idx1, 1);
              return false;
            }
            return true;
          });
        }
        return true;
      });
    }
    this._refreshStatus();
  };

  _startValueChange = () => {
    if (this._startDate > this._endDate) {
      this._endDate = null;
    }
  }
  _endValueChange = () => {
    if (this._startDate > this._endDate) {
      this._startDate = null;
    }
  }
  _disabledStartDate = (startValue) => {
    const now = new Date();
    now.setHours(0, 0, 0, 0);
    const now_time = now.getTime();
    if (!startValue || !this._endDate) {
      return startValue > now_time;
    }
    // return (startValue.getTime() >= this._endDate.getTime()) || (startValue.getTime() <= Date.now());
    return startValue.getTime() > this._endDate.getTime();
  }
  _disabledEndDate = (endValue) => {
    const now = new Date();
    now.setHours(0, 0, 0, 0);
    const now_time = now.getTime();
    if (!endValue || !this._startDate) {
      return endValue > now_time;
    }
    return (endValue.getTime() < this._startDate.getTime()) || (endValue > now_time);
  }

  ngOnInit() {
    this.getList();
  }

  getList() {
    this.loading = true;
    this.startDate = this._startDate ? moment(this._startDate).format('YYYY-MM-DD 00:00:00') : '';
    this.endDate = this._endDate ? moment(this._endDate).format('YYYY-MM-DD 23:59:59') : '';
    this.accountService.getTopupList(
      this.page,
      this.pageSize,
      this.pay_type,
      this.startDate,
      this.endDate,
      this.sort,
      this.filterStatus).subscribe(res => {
      if (res.code === 2000) {
        this.total = res.data.total;
        this.listData = res.data.data;
        if (!!this.checkedIds.length) {
          this.listData.every((val, idx, array) => {
            this.checkedIds.every((val1, idx1, array1) => {
              if (val.order_no === val1) {
                val.checked = true;
                return false;
              } else {
                val.checked = false;
                return true;
              }
            });
            return true;
          });
          this._refreshStatus();
        }
      } else if (res.code === 4040 && res.message === '未开通推客!') {

      } else {
        this._message.create('error', res.message);
      }
      this.loading = false;
    });
  }

  reset() {
    this._startDate = null;
    this._endDate = null;
    this.pay_type = 0;
    this.page = 1;
    this.getList();
  }

  resetStatus() {
    this.filterStatus.every((val, idx, array) => {
      val.checked = false;
      return true;
    });
    this.getList();
  }

  sortData(name, status) {
    this._indeterminate = false;
    this._allChecked = false;
    this.checkedIds = [];
    this.sort[name] = status;
    this.getList();
  }


  // 按结果导出
  exportChecked(type) {  // type 1: 全部导出 2: 勾选导出
    let data;
    if (type === 1) {
      this.startDate = this._startDate ? moment(this._startDate).format('YYYY-MM-DD 00:00:00') : '';
      this.endDate = this._endDate ? moment(this._endDate).format('YYYY-MM-DD 23:59:59') : '';
      data = {
        pay_time_start: this.startDate,
        pay_time_end: this.endDate
      };
      if (!!this.pay_type) { data.pay_type = this.pay_type; }
    } else {
      if (!this.checkedIds.length) {
        this.confirmServ.error({
          title: '下载订单失败',
          content: '您未勾选任何订单，请勾选需要下载的订单'
        });
        return;
      }
      data = {
        order_no: JSON.stringify(this.checkedIds)
      };
    }
    const link = document.createElement('a');
    this.accountService.down_xsl_checked(data).subscribe( res => {
      link.setAttribute('href', window.URL.createObjectURL(res));
      const now = moment().format('YYYY-MM-DD h:mm:ss');
      link.setAttribute('download', '充值列表' + now + '.xls');
      link.style.visibility = 'hidden';
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    });
  }
}
